<template>
	<basic-container>
		<avue-crud :option="option" :data="data" :page="page" ref="crud" @search-change="searchChange" @search-reset="searchReset"
		 @current-change="currentChange" @size-change="sizeChange" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave"
		 @on-load="onLoad">
		</avue-crud>
	</basic-container>
</template>

<script>
	import request from '@/router/axios';

	export default {
		data() {
			return {
				query: {},
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				option: {
					searchMenuSpan: 4,
					border: true,
					index: true,
					refreshBtn: false,
					excelBtn: true,
					menuWidth: '150px',
					column: [{
							label: "库位编号",
							prop: "localCode",
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "库位名称",
							prop: "localName",
							searchPlaceholder: '编号&名称',
							search: true,
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "所属仓库",
							prop: "storeCode",
							type: "select",
							dicData: [],
							searchFilterable: true,
							search: true,
							props: {
								label: "storeName",
								value: "storeCode"
							},
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "库位类型",
							prop: "localType",
							type: "select",
							dicData: [],
							searchFilterable: true,
							search: true,
							props: {
								label: "name",
								value: "sort"
							},
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "排序",
							prop: "sort",
							type: 'number',
							width: 100
						},
						{
							label: "备注",
							prop: "remark",
							span: 24
						}
					]
				},
				data: []
			};
		},
		created() {
			request({
				url: '/api/code/getCodes',
				params: {
					typeId: 'CODE200712'
				}
			}).then(res => {
				this.findObject(this.option.column, 'localType').dicData = res.data.data;
			});
			request({
				url: '2/api/WmsStorage/getpages',
				params: {
					limit: 0
				}
			}).then(res => {
				this.findObject(this.option.column, 'storeCode').dicData = res.data.data;
			});
		},
		methods: {
			rowSave(row, loading, done) {
				request({
					url: '2/api/WmsLocation/add',
					method: 'post',
					data: row
				}).then(() => {
					console.log(1)
					loading();
					this.onLoad(this.page);
					done();
				}, error => {
					done();
					console.log(error);
				});
			},
			rowUpdate(row, index, loading, done) {
				return request({
					url: '2/api/WmsLocation/edit',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.onLoad(this.page);
				}, error => {
					done();
					console.log(error);
				});
			},
			rowDel(row) {
				this.$confirm("确定数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return request({
							url: '2/api/WmsLocation/remove',
							method: 'post',
							params: {
								ids: row.id
							}
						});
					})
					.then(() => {
						this.onLoad(this.page);
					});
			},
			searchReset() {
				this.query = {};
				this.page.currentPage = 1;
				this.onLoad(this.page);
			},
			searchChange(params, done) {
				this.page.currentPage = 1;
				this.query = params;
				this.onLoad(this.page);
				done();
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			onLoad(page) {
				request({
					url: '2/api/WmsLocation/getpages',
					params: {
						key: this.query.localName == undefined ? '' : this.query.localName,
						type: this.query.storeCode == undefined ? '' : this.query.storeCode,
						status: this.query.localType == undefined ? '' : this.query.localType,
						page: page.currentPage,
						limit: page.pageSize
					}
				}).then(res => {
					const data = res.data
					this.data = data.data;
					this.page.total = data.remark;
				});
			}
		}
	};
</script>

<style>
</style>
